<?php

class Bs_admin {
	public function __construct() {
	   add_action( 'admin_enqueue_scripts', array(&$this, 'bs_admin_enqueue_scripts') );
	   add_action( 'admin_menu', array( &$this, 'cw_boot_by_player_submenu' ),11);
	   add_action( 'wp_ajax_my_action', array( &$this, 'my_action_callback') );
	   add_action( 'admin_init', array( &$this, 'save_data' ) );
	}
	
	/**
	 * Add stylesheets and scripts only for admin
	 */
	public function bs_admin_enqueue_scripts($hook)
	{
	   wp_register_style( 'bs-admin-css', BS_ASSETS_URL . 'css/admin.css', false, '1.0.0' );
	   wp_enqueue_style( 'bs-admin-css' );
	   
	   wp_enqueue_script( 'bs-admin-js', BS_ASSETS_URL. 'js/admin.js', array('jquery'), '1.0.0' );
	}

	public function cw_boot_by_player_submenu() {
		add_submenu_page('ff_options', __('Boot by player', 'cleverweb'), __('Boot by player', 'cleverweb'), 'manage_options', 'bootbyplayer.php', array( &$this, 'get_boot_by_player' ));
	}

	public function get_boot_by_player() {

		global $wpdb;

		$players_pr_page = 20;

		?>

		<h1>Boot by Player</h1>

		<?php

		//$players = $wpdb->get_results("SELECT * FROM `ff_wp_players` WHERE name = '' OR boot = '' OR nation = '' OR club = '' OR position = '' OR foot = '' OR fClubs = '' ORDER BY `name` LIMIT 0," . $players_pr_page . ";");
		$players_missing_info = $this->get_player_info(true);

		$players_missing_info_total = $this->get_player_count(true);

		$players_with_all_info = $this->get_player_info(false);

		$players_with_all_info_total = $this->get_player_count(false);

		$players_missing_info_pages = floor($players_missing_info_total[0]->total_players/$players_pr_page);

		$players_with_all_info_pages = floor($players_with_all_info_total[0]->total_players/$players_pr_page);

		$table_header_missing_info = '<h2>Spillere med manglende info:</h2>';

		$table_header_all_info = '<h2>Spillere med alt info:</h2>';

		$table_missing_info = '<table id="player-table-missing-info">';

		$table_with_all_info = '<table id="player-table-with-all-info">';

		$spans_missing_info = 'empty';

		$spans_with_all_info = 'not-empty';

		//$pages = floor($players_total[0]->total_players/$players_pr_page);

		if($players_missing_info_total[0]->total_players % $players_pr_page)
			$players_missing_info_pages++;

		if($players_with_all_info_total[0]->total_players % $players_pr_page)
			$players_with_all_info_pages++;

		$this->get_table_html($players_missing_info, $players_missing_info_pages, $table_header_missing_info, $table_missing_info, $spans_missing_info);

		$this->get_table_html($players_with_all_info, $players_with_all_info_pages, $table_header_all_info, $table_with_all_info, $spans_with_all_info);
    }

    public function my_action_callback() {
		global $wpdb;
		$start = intval( $_POST['start'] );
		$length = intval( $_POST['length'] );
		$type = $_POST['type'];
		if($type == 'empty') {
			$result = $wpdb->get_results("SELECT * FROM `ff_wp_players` WHERE name = '' OR boot = '' OR nation = '' OR club = '' OR position = '' OR foot = '' OR fClubs = '' ORDER BY `name` LIMIT " . $start . "," . $length . ";");
		}
		else {
			$result = $wpdb->get_results("SELECT * FROM `ff_wp_players` WHERE name != '' AND boot != '' AND nation != '' AND club != '' AND position != '' AND foot != '' AND fClubs != '' ORDER BY `name` LIMIT " . $start . "," . $length . ";");
		}
		foreach($result as $res)
		{
			unset($res->html);
		}
		echo json_encode($result);
		die();
	}

	public function get_player_info($with_missing_info) {
		global $wpdb;
		$players_pr_page = 20;
		if($with_missing_info === true) {
			$players = $wpdb->get_results("SELECT * FROM `ff_wp_players` WHERE name = '' OR boot = '' OR nation = '' OR club = '' OR position = '' OR foot = '' OR fClubs = '' ORDER BY `name` LIMIT 0," . $players_pr_page . ";");
		}
		else {
			$players = $wpdb->get_results("SELECT * FROM `ff_wp_players` WHERE name != '' AND boot != '' AND nation != '' AND club != '' AND position != '' AND foot != '' AND fClubs != '' ORDER BY `name` LIMIT 0," . $players_pr_page . ";");
		}
		return $players;
	}

	public function get_player_count($with_missing_info) {
		global $wpdb;
		if($with_missing_info === true) {
			$player_count = $wpdb->get_results("SELECT COUNT(*) AS total_players FROM `ff_wp_players` WHERE name = '' OR boot = '' OR nation = '' OR club = '' OR position = '' OR foot = '' OR fClubs = '';");
		}
		else {
			$player_count = $wpdb->get_results("SELECT COUNT(*) AS total_players FROM `ff_wp_players` WHERE name != '' AND boot != '' AND nation != '' AND club != '' AND position != '' AND foot != '' AND fClubs != '';");
		}
		return $player_count;
	}

	public function get_table_html($players, $pages, $header, $table, $span) {

		?>
		<div class='player-paging'>
		<?php for($i = 1; $i <= $pages; $i++)
		{
			echo '<span data-type="' . $span . '" data-page="' . $i . '">'. $i .'</span>';
		}
		?>
		</div>
		<form method="POST">
		<?php wp_nonce_field('ff_action', 'ff_nonce');
		echo $header;
		echo $table; ?>
	    <thead>
		<tr>
		   <!--<th>ID</th>-->
		   <th>Navn</th>
		   <th>Støvle</th>
		   <th>Land</th>
		   <th>Klub</th>
		   <th>Position</th>
		   <th>Ben</th>
		   <th>Tidligere klubber</th>
		</tr>
	   </thead>
	   <tbody>

	   <?php
	   foreach($players as $player)
	   {
		$results = json_decode($player->results);
		?>
			<tr>
			   <!--<td><?php echo $club->club_id ?></td>-->
			   <td><input type="text" name="players[<?php echo $player->id; ?>][player_name]" value="<?php echo $player->name ?>"></td>
			   <td><input type="text" name="players[<?php echo $player->id; ?>][player_boot]" value="<?php echo $player->boot ?>"></td>
			   <td><input type="text" name="players[<?php echo $player->id; ?>][player_nation]" value="<?php echo $player->nation ?>"></td>
			   <td><input type="text" name="players[<?php echo $player->id; ?>][player_club]" value="<?php echo $player->club ?>"></td>
			   <td><input type="text" name="players[<?php echo $player->id; ?>][player_position]" value="<?php echo $player->position ?>"></td>
			   <td><input type="text" name="players[<?php echo $player->id; ?>][player_foot]" value="<?php echo $player->foot ?>"></td>
			   <td><textarea name="players[<?php echo $player->id; ?>][player_fClubs]"><?php echo $player->fClubs ?></textarea></td>
			</tr>
		
		<?php
		}
		?>
		</tbody>
	</table>
	<input name="save-button" type="submit" class="save-button" value="Gem">
</form>
	<?php
	}

	public function save_data() {

		if ( isset( $_POST['ff_nonce'] ) && wp_verify_nonce( $_POST['ff_nonce'], 'ff_action' ) ) {
		
   		global $wpdb;

			foreach($_POST['players'] as $player_id => $data) {
				$wpdb->update('ff_wp_players', array('name' => $_POST['players'][$player_id]['player_name'], 'boot' => $_POST['players'][$player_id]['player_boot'], 'nation' => $_POST['players'][$player_id]['player_nation'], 'club' => $_POST['players'][$player_id]['player_club'], 'position' => $_POST['players'][$player_id]['player_position'], 'foot' => $_POST['players'][$player_id]['player_foot'], 'fClubs' => $_POST['players'][$player_id]['player_fClubs']), array('id' => $player_id));
			}
		} else if (isset( $_POST['ff_nonce'] ) && !wp_verify_nonce( $_POST['ff_nonce'], 'ff_action' ) ) {
			echo 'Der er opstået en fejl';
		}
	}
}

new Bs_admin(); 